package com.devunion.bogdan.view.util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

/**
 * User: user
 * Date: 03.11.2004
 * Time: 23:01:29
 */
public class DialogHelper {

    //, int ipadx, int ipady    
    public static void placeComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, int insetTop, int insetLeft, int insetRight, int insetBottom) {
        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = gridx;
        constraints.gridy = gridy;

        constraints.gridwidth = gridwidth;
        constraints.gridheight = gridheight;

        constraints.weightx = weightx;
        constraints.weighty = weighty;

        constraints.anchor = anchor;
        constraints.fill = fill;

        if (insetTop != 0 || insetBottom != 0 || insetLeft != 0 || insetRight != 0) {
            constraints.insets = new Insets(insetTop, insetLeft, insetBottom, insetRight);
        }

        container.add(component, constraints);
    }

    public static Frame getMainFrame() {
        Frame[] frame = Frame.getFrames();
        for (Frame aFrame : frame) {
            if (aFrame.getOwner() != null)
                continue;
            return aFrame;
        }
        return null;
    }

    public static void setOkKeyboardAction(JDialog jDialog, ActionListener actionListener) {
        ((JComponent)jDialog.getContentPane()).registerKeyboardAction(actionListener, "pressed", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
        ((JComponent)jDialog.getContentPane()).registerKeyboardAction(actionListener, "pressed", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    public static void setCancelKeyboardAction(JDialog jDialog, ActionListener actionListener) {
        ((JComponent)jDialog.getContentPane()).registerKeyboardAction(actionListener, "pressed", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED);
        ((JComponent)jDialog.getContentPane()).registerKeyboardAction(actionListener, "pressed", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }
}
